package org.example.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.mapper.ClazzMapper;
import org.example.pojo.Clazz;
import org.example.pojo.ClazzQueryParam;
import org.example.pojo.PageResult;
import org.example.pojo.Result;
import org.example.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;

    /**
     * 查询所有班级
     * @return
     */
    @Override
    public List<Clazz> findAll() {
        List<Clazz> clazzList=clazzMapper.selectAll();
        return clazzList;
    }


    /**
     * 分页查询班级
     * @param clazzQueryParam
     * @return
     */
    @Override
    public PageResult pageQuery(ClazzQueryParam clazzQueryParam) {
        PageHelper.startPage(clazzQueryParam.getPage(),clazzQueryParam.getPageSize());
        Page<Clazz> page=clazzMapper.pageQuery(clazzQueryParam);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id删除班级
     * @param id
     */
    @Override
    @Transactional
    public void deleteById(Integer id) {
        clazzMapper.deleteById(id);
    }

    /**
     * 根据id查询班级
     * @param id
     * @return
     */
    @Override
    public Clazz selectById(Integer id) {
        Clazz clazz=clazzMapper.selectById(id);
        return clazz;
    }

    /**
     * 修改班级信息
     * @param clazz
     */
    @Override
    @Transactional
    public void update(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.update(clazz);
    }

    /**
     * 添加班级
     * @param clazz
     */
    @Transactional
    @Override
    public void insert(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.insert(clazz);
    }


}
